using System;
using FixMath;
using UniFramework.Actor;
using UniFramework.Aoe;
using UniFramework.Battle;
using UniFramework.Buff;
using UnityEngine;

/// <summary>
/// buff生命周期测试
/// </summary>
public class GameMain : MonoBehaviour
{
	private LogicActor _actor;
	private LogicActor _actor2;
	private LogicActor _actor3;
	private LogicActor _actor4;
	public DamageSystem damageSystem;
	public BulletSystem bulletSystem;
	public AoeSystem aoeSystem;
	public int damageValue = 0;
	public GameObject bulletObj;
	public static Action InputAKeyEvent;

	private void Start()
	{
		damageSystem = gameObject.AddComponent<DamageSystem>();
		bulletSystem = gameObject.AddComponent<BulletSystem>();
		aoeSystem = gameObject.AddComponent<AoeSystem>();
		_actor = GameObject.Find("Player").GetComponent<LogicActor>();
		_actor2 = GameObject.Find("Player2").GetComponent<LogicActor>();
		_actor3 = GameObject.Find("Player3").GetComponent<LogicActor>();
		_actor4 = GameObject.Find("Player4").GetComponent<LogicActor>();
		_actor.OnCreate();
		_actor2.OnCreate();
		_actor3.OnCreate();
		_actor4.OnCreate();

		AddActorDieBuff(_actor2);
	}

	private void Update()
	{
		if (Input.GetKeyDown(KeyCode.A))
		{
			InputAKeyEvent?.Invoke();
			if (_actor.GetComponent<LogicActorState>().state.isInput)
			{
				TeleportTest();
			}
		}

		if (Input.GetKeyDown(KeyCode.Space))
		{
			AddBulletTest();
		}
	}

	private void FixedUpdate()
	{
		_actor?.OnLogicFrameUpdate(Time.frameCount);
		_actor2?.OnLogicFrameUpdate(Time.frameCount);
		_actor3?.OnLogicFrameUpdate(Time.frameCount);
		_actor4?.OnLogicFrameUpdate(Time.frameCount);
		damageSystem?.OnLogicFrameUpdate();
		bulletSystem.OnLogicFrameUpdate();
		aoeSystem.OnLogicFrameUpdate();
	}

	#region buff

	private void AddTestBuff()
	{
		var buffConfig = new BuffConfig("1", "test", null, 1, 1, (FixInt)LogicFrameConfig.logicFrameInterval,
			onOccur: "OnOccurFunc", onTick: "OnTickedFunc", beHurt: "OnBeHit",
			beKilled: "OnBeKilled", controlState: LogicActorControlState.defalut);
		AddBuffInfo addBuffInfo = new AddBuffInfo(buffConfig, null, _actor, 1, 1, permanent: true);
		var buffSystem = _actor.GetComponent<BuffSystem>();
		buffSystem.AddBuff(addBuffInfo);
	}

	private void AddActorDieBuff(LogicActor actor)
	{
		var buffConfig = new BuffConfig()
		{
			onBeKilled = BuffData.onBeKilledFunc["OnActorBeKilled"],
		};
		AddBuffInfo addBuffInfo = new AddBuffInfo(buffConfig, null, _actor, 1, 1, permanent: true);
		var bindActor = GetNextActor(actor);
		addBuffInfo.buffParam = new()
		{
			{ "BindActor", bindActor },
		};
		var buffSystem = actor.GetComponent<BuffSystem>();
		buffSystem.AddBuff(addBuffInfo);
	}


	public LogicActor GetNextActor(LogicActor actor)
	{
		if (actor == _actor2) return _actor3;
		if (actor == _actor3) return _actor4;
		return null;
	}

	#endregion

	#region bullet

	private void AddBulletTest()
	{
		BulletConfig bulletConfig = new BulletConfig()
		{
			bulletPrefab = bulletObj,
			radius = 2,
			sameTargetDelayCheck = 10,
			hitEnemy = true,
			onCreate = BulletData.onCreateFunc["OnBulletCreateTest"],
			onHit = BulletData.onHitFunc["BulletOnHitTest"],
			onRemoved = BulletData.onRemovedFunc["OnRemoveBulletTest"],
			cnaHitCnt = 1,
			deleteHp = 50,
		};
		FixIntVector3 firePos = new FixIntVector3(_actor.transform.position) + new FixIntVector3(0, 0, 3);
		BulletObj obj = new BulletObj()
		{
			config = bulletConfig,
			release = _actor,
			firePos = firePos,
			speed = new FixInt(1f),
			fireAngle = new FixIntVector3(_actor.transform.forward),
			duration = 50
		};
		bulletSystem.CreateBullet(obj);
	}

	/// <summary>
	/// 测试发射手雷
	/// </summary>
	private void GrenadeTest()
	{
		BulletConfig bulletConfig = new BulletConfig()
		{
			bulletPrefab = bulletObj,
			radius = 2,
			sameTargetDelayCheck = 10,
			hitEnemy = true,
			onCreate = BulletData.onCreateFunc["OnBulletCreateTest"],
			onHit = BulletData.onHitFunc["BulletOnHitTest"],
			onRemoved = BulletData.onRemovedFunc["OnRemoveBulletBeginAoe"],
			cnaHitCnt = 2
		};
		FixIntVector3 firePos = new FixIntVector3(_actor.transform.position) + new FixIntVector3(0, 0, 3);
		BulletObj obj = new BulletObj()
		{
			config = bulletConfig,
			release = _actor,
			firePos = firePos,
			speed = new FixInt(0.1f),
			fireAngle = new FixIntVector3(_actor.transform.forward),
			duration = 50
		};
		bulletSystem.CreateBullet(obj);
	}

	/// <summary>
	/// 瞬移子弹测试
	/// 点击空格发射子弹，再点击一下进行移动
	/// </summary>
	private void TeleportTest()
	{
		BulletConfig bulletConfig = new BulletConfig()
		{
			bulletPrefab = bulletObj,
			radius = 2,
			sameTargetDelayCheck = 10,
			hitEnemy = true,
			onCreate = BulletData.onCreateFunc["OnBulletCreateTest"],
			onHit = BulletData.onHitFunc["BulletOnHitTest"],
			onRemoved = BulletData.onRemovedFunc["OnRemoveBulletTest"],
			cnaHitCnt = 2,
		};
		FixIntVector3 firePos = new FixIntVector3(_actor.transform.position) + new FixIntVector3(0, 0, 3);
		BulletObj obj = new BulletObj()
		{
			config = bulletConfig,
			release = _actor,
			firePos = firePos,
			speed = new FixInt(0.1f),
			fireAngle = new FixIntVector3(_actor.transform.forward),
			duration = 100,
		};
		var logicBullet = bulletSystem.CreateBullet(obj);
		var buffSystem = _actor.GetComponent<BuffSystem>();
		BuffConfig notCanInputBuff = new BuffConfig()
		{
			id = "test_buff",
			tickTime = 0.02f,
			controlState = new LogicActorControlState(true, true, true, false),
			onOccur = BuffData.onOccurFunc["OnNotCanInputBuff"],
			onRemoved = BuffData.onRemovedFunc["OnNotCanInputBuffEnd"],
			onTick = BuffData.onTickFunc["OnCheckBullet"],
		};
		AddBuffInfo addBuffInfo = new AddBuffInfo()
		{
			buffConfig = notCanInputBuff,
			target = _actor,
			permanent = true,
			buffParam = new()
			{
				{ "LogicBullet", logicBullet }
			}
		};

		buffSystem.AddBuff(addBuffInfo);
	}

	#endregion

	#region damage

	private void DoDamage()
	{
		Damage damage = new Damage(damageValue);
		damageSystem.DoDamage(null, _actor, damage);
	}

	#endregion
}